<?php
	/**
	 *	Copyright (c) TeamFlamingo And gray 2004-2009
	 *	www.me-live.com.cn / www.i-gray.cn
	 *
	 *	[ WARNING ]
	 *		This is NOT a freeware!
	 *		You MUST get author's authorization before using it!
	 *	[ WARNING ]
	 *
	 *	Name : MDKmemcached.lib.php / Development Code Evolve
	 *	Created / Modify : 2010-3-17 / --
	 */

	if( !defined( 'IN_TEAME' ) )
	{
		exit( 'Access Denied' );
	}

	class	MDKmemcached
	{
		public	$MC			=	null;						//	[CH]	定义memcached对象
		public	$runTime	=	false;						//	[CH]	当前memcached运行环境

		/**
		 *	构造函数
		 *	@param	Array	$arr
		 */
		public	function	__construct( $arr )
		{
			//	[CH]	如果未启用Memcached
			//	[CH]	直接返回
			if( !ME_MEMCACHED_START )
				return $this->MC	=	false;

			if( !MEMCACHED_MODULE_LOADED )
			{
				include	'./extension/memcached-client.php';
				$this->runTime	=	'client';
				return	$this->MC	=	new memcached( $arr );
			}
			else
			{
				$server	=	explode( ':', $arr['servers'][0] );
				$this->runTime	=	'server';
				return	$this->MC	=	memcache_pconnect( $server[0], $server[1] );
			}
		}

		/**
		 *	向Memcached添加对象
		 *	@param	String	$key	资源标识符
		 *	@param	*		$val	数据
		 *	@param	Int		$exp	有效期
		 */
		public	function	add( $key, $val, $exp = 0 )
		{
			if( !$this->MC )
				return false;

			if( !MEMCACHED_MODULE_LOADED )
				return	$this->MC->add( $key, $val, $exp );
			else
				return	memcache_add( $this->MC, $key, $val, false, $exp );
		}

		/**
		 *	获取Memcached对象
		 *	@param	String	$key	资源句柄
		 */
		public	function	get( $key )
		{
			if( !$this->MC )
				return false;

			if( !MEMCACHED_MODULE_LOADED )
				return	$this->MC->get( $key );
			else
				return	memcache_get( $this->MC, $key );
		}

		public	function	set( $key, $val, $exp = 0 )
		{
			if( !$this->MC )
				return false;

			if( !MEMCACHED_MODULE_LOADED )
				return	$this->MC->set( $key, $val, $exp );
			else
				return	memcache_set( $this->MC, $key, $val, MEMCACHE_COMPRESSED, $exp );
		}

		/**
		 *	替换Memcached对象
		 *	@param	String	$key	资源句柄
		 *	@param	*		$val	数据
		 *	@param	Int		$exp	有效期
		 */
		public	function	replace( $key, $val, $exp = 0 )
		{
			if( !$this->MC )
				return false;

			if( !MEMCACHED_MODULE_LOADED )
				return	$this->MC->replace( $key, $val, $exp );
			else
				return	memcache_replace( $this->MC, $key, $val, false, $exp );
		}

		public	function	delete( $key, $timeout = 10 )
		{
			if( !$this->MC )
				return false;

			if( !MEMCACHED_MODULE_LOADED )
				return	$this->MC->delete( $key, $timeout );
			else
				return	memcache_delete( $key, $timeout );
		}
	}
?>